﻿using Sirenix.OdinInspector;
using UnityEngine;

namespace Gp.Scripts.Character {
    public class FovDrawer: MonoBehaviour {
        [SerializeField]
        [LabelText("FOV 组件")]
        private FovSight fovSight;
        private void OnDrawGizmos() {
            Gizmos.color = Color.green;
            
            float dist = fovSight.FarDistance / Mathf.Cos(fovSight.HalfVerticalAngle * Mathf.Deg2Rad)
                                              / Mathf.Cos(fovSight.HalfHorizontalAngle * Mathf.Deg2Rad);
    
            Vector3 originDir = transform.forward;
            Quaternion topQua = Quaternion.AngleAxis(fovSight.HalfVerticalAngle, Vector3.left);
            Quaternion bottomQua = Quaternion.AngleAxis(-fovSight.HalfVerticalAngle, Vector3.left);
            Quaternion rightQua = Quaternion.AngleAxis(fovSight.HalfHorizontalAngle, Vector3.up);
            Quaternion leftQua = Quaternion.AngleAxis(-fovSight.HalfHorizontalAngle, Vector3.up);

            Vector3 topLeftDir = topQua * leftQua * originDir;
            Vector3 topRightDir = topQua * rightQua * originDir;
            Vector3 bottomLeftDir = bottomQua * leftQua * originDir;
            Vector3 bottomRightDir = bottomQua * rightQua * originDir;
    
            Vector3 originPos = transform.position;
            Vector3 topLeftPos = originPos + topLeftDir * dist;
            Vector3 topRightPos = originPos + topRightDir * dist;
            Vector3 bottomLeftPos = originPos + bottomLeftDir * dist;
            Vector3 bottomRightPos = originPos + bottomRightDir * dist;
    
            Gizmos.DrawLine(originPos, topLeftPos);
            Gizmos.DrawLine(originPos, topRightPos);
            Gizmos.DrawLine(originPos, bottomLeftPos);
            Gizmos.DrawLine(originPos, bottomRightPos);
    
            Gizmos.DrawLine(topLeftPos, topRightPos);
            Gizmos.DrawLine(topRightPos, bottomRightPos);
            Gizmos.DrawLine(bottomRightPos, bottomLeftPos);
            Gizmos.DrawLine(bottomLeftPos, topLeftPos);
        }
    }
}